<?php
IncludeFunction('productcat');

function ListOptionTrade($select)
{
global $dbsql,$language;

	$sql_select="select iId,cName from tbltrade where cLanguage='$language'";
	$rs=$dbsql->query($sql_select);
	
	$options="<option value=-1>--- none ---</option>";
	while($result=$dbsql->fetch_array($rs))
	{
		if($result['iId']==$select)
		{
			$options.="<option value='".$result['iId']."' selected>".$result['cName']."</option>";
		}else
		{
			$options.="<option value='".$result['iId']."'>".$result['cName']."</option>";
		}
	}
	return $options;
}
function ProductById($id)
{
	global $dbsql,$language,$type;
	//-----------------------------------------------------
	$sql_select = "SELECT * FROM tblproduct WHERE iId = $id and cLanguage='$language' ";
	
	$rs=$dbsql->query($sql_select);
	if($dbsql->num_rows($rs)==0)
	{
		$msg = "Not Exit !";
		$page = "?";
		page_transfer($msg,$page);
	}
	$content           = $dbsql->fetch_array($rs);
	$content['cTag']   = Select_Tag($id,'Product%');
    return $content;
}


function LoadProductById($id)
{
global $dbsql;
	$sql_select="select * from tblproduct where iId=$id";
	$rs=$dbsql->query($sql_select);
	$result=$dbsql->fetch_array($rs);
	return $result;
}

function ListProductByCatId($idCat,$search="")
{
global $dbsql,$language;

	$count_all	=CountProductByCatId($idCat,$search);	
	list($limit_start,$limit_all)	=Slit_Page($count_all);	
	$listcontent=array();
	
	//-----------------------------------------------------
	$sql_select = "select iId,iParentId,cName,iStatus,iOrder,cPrice,iCountView,iDepository,iTypical,iNew,iHot ";
	$sql_select.=" from tblproduct where iId>0 ";
	if($idCat>0)
    {
        $sql_select.=" and iParentId = '$idCat'";
    }
	if(!empty($search))
    {
        $sql_select.=" and cName like '$search%'";
    }
	$sql_select.=" and cLanguage='$language' order by iOrder asc ";
	$sql_select.="  limit $limit_start,$limit_all";

	$rs=$dbsql->query($sql_select);
	while ( $result = $dbsql->fetch_array($rs) )
	{
		$listcontent[] = $result;
	}
	return $listcontent;
}



function CountProductByCatId($idCat,$search="")
{
global $dbsql,$language;

	$sql_select="select count(iId) as count from tblproduct where iId>0 and cLanguage='$language'";
	if($idCat>0)$sql_select.=" and iParentId=$idCat";
	if(!empty($search))$sql_select.=" and cName like '$search%'";
	$rs=$dbsql->query($sql_select);
	$result=$dbsql->fetch_array($rs);
	return $result['count'];
}
?>